/**
 * MenuFrameChooseBanner.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2011 Roslan Amir. All rights reserved.
 */

package com.roslan.games.moo3d.ui.menu;

import com.ardor3d.extension.ui.UIButton;
import com.ardor3d.extension.ui.UILabel;
import com.ardor3d.extension.ui.UIPanel;
import com.ardor3d.extension.ui.UIRadioButton;
import com.ardor3d.extension.ui.event.ActionEvent;
import com.ardor3d.extension.ui.event.ActionListener;
import com.ardor3d.extension.ui.layout.GridLayout;
import com.ardor3d.extension.ui.layout.GridLayoutData;
import com.ardor3d.extension.ui.layout.RowLayout;
import com.ardor3d.extension.ui.util.Alignment;
import com.ardor3d.extension.ui.util.ButtonGroup;
import com.roslan.games.moo3d.GameData.Actions;

/**
 * Comments go here.
 * 
 * @author Roslan Amir
 * @version 1.0 - Jan 27, 2011
 */
public class MenuFrameChooseBanner extends MenuFrameBase {

	/**
	 * Default no-arg constructor.
	 */
	public MenuFrameChooseBanner() {
		super("Choose Banner");

		/*
		 * Race Image
		 */
		final UILabel raceImageLabel = new UILabel("");
		raceImageLabel.setMargin(FRAME_MARGINS);
		raceImageLabel.setBorder(SOLID_BORDER);
		raceImageLabel.setAlignment(Alignment.MIDDLE);
		raceImageLabel.setIcon(SUBTEX_RACE_HEADS[gameData.playerRaceIndex]);
		raceImageLabel.updateIconDimensionsFromIcon();
		vpanel.add(raceImageLabel);

		/*
		 * Panel with a horizontal layout to hold the banner image and radio buttons
		 */
		final UIPanel hpanel1 = new UIPanel();
		hpanel1.setLayout(new RowLayout(true));
		hpanel1.setBorder(SOLID_BORDER);
		vpanel.add(hpanel1);

		/*
		 * Race FleetBanner Image
		 */
		final UILabel bannerImageLabel = new UILabel("");
		bannerImageLabel.setMargin(FRAME_MARGINS);
		bannerImageLabel.setBorder(SOLID_BORDER);
		bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
		bannerImageLabel.updateIconDimensionsFromIcon();
		hpanel1.add(bannerImageLabel);

		/*
		 * Panel with a Grid layout
		 */
		final UIPanel grid = new UIPanel();
		grid.setLayout(new GridLayout());
		grid.setMargin(FRAME_MARGINS);
		grid.setBorder(SOLID_BORDER);
		hpanel1.add(grid);

		/*
		 * Race Banners (Colors)
		 */
		final ButtonGroup chooseBannerGroup = new ButtonGroup();
		final UIRadioButton chooseBannerRed = new UIRadioButton("[b]Red[/b]");
		chooseBannerRed.setStyledText(true);
		chooseBannerRed.setSelectable(true);
		chooseBannerRed.setSelected(gameData.playerBannerIndex == 0);
		chooseBannerRed.setGroup(chooseBannerGroup);
		grid.add(chooseBannerRed);

		final UIRadioButton chooseBannerYellow = new UIRadioButton("[b]Yellow[/b]");
		chooseBannerYellow.setStyledText(true);
		chooseBannerYellow.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseBannerYellow.setSelectable(true);
		chooseBannerYellow.setSelected(gameData.playerBannerIndex == 1);
		chooseBannerYellow.setGroup(chooseBannerGroup);
		grid.add(chooseBannerYellow);

		final UIRadioButton chooseBannerGreen = new UIRadioButton("[b]Green[/b]");
		chooseBannerGreen.setStyledText(true);
		chooseBannerGreen.setSelectable(true);
		chooseBannerGreen.setSelected(gameData.playerBannerIndex == 2);
		chooseBannerGreen.setGroup(chooseBannerGroup);
		grid.add(chooseBannerGreen);

		final UIRadioButton chooseBannerGrey = new UIRadioButton("[b]Grey[/b]");
		chooseBannerGrey.setStyledText(true);
		chooseBannerGrey.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseBannerGrey.setSelectable(true);
		chooseBannerGrey.setSelected(gameData.playerBannerIndex == 3);
		chooseBannerGrey.setGroup(chooseBannerGroup);
		grid.add(chooseBannerGrey);

		final UIRadioButton chooseBannerBlue = new UIRadioButton("[b]Blue[/b]");
		chooseBannerBlue.setStyledText(true);
		chooseBannerBlue.setSelectable(true);
		chooseBannerBlue.setSelected(gameData.playerBannerIndex == 4);
		chooseBannerBlue.setGroup(chooseBannerGroup);
		grid.add(chooseBannerBlue);

		final UIRadioButton chooseBannerBrown = new UIRadioButton("[b]Brown[/b]");
		chooseBannerBrown.setStyledText(true);
		chooseBannerBrown.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseBannerBrown.setSelectable(true);
		chooseBannerBrown.setSelected(gameData.playerBannerIndex == 5);
		chooseBannerBrown.setGroup(chooseBannerGroup);
		grid.add(chooseBannerBrown);

		final UIRadioButton chooseBannerPurple = new UIRadioButton("[b]Purple[/b]");
		chooseBannerPurple.setStyledText(true);
		chooseBannerPurple.setSelectable(true);
		chooseBannerPurple.setSelected(gameData.playerBannerIndex == 6);
		chooseBannerPurple.setGroup(chooseBannerGroup);
		grid.add(chooseBannerPurple);

		final UIRadioButton chooseBannerOrange = new UIRadioButton("[b]Orange[/b]");
		chooseBannerOrange.setStyledText(true);
		chooseBannerOrange.setSelectable(true);
		chooseBannerOrange.setSelected(gameData.playerBannerIndex == 7);
		chooseBannerOrange.setGroup(chooseBannerGroup);
		grid.add(chooseBannerOrange);

		/*
		 * Add listeners to each radio button
		 */
		chooseBannerRed.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 0;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerYellow.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 1;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerGreen.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 2;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerGrey.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 3;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerBlue.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 4;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerBrown.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 5;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerPurple.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 6;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});
		chooseBannerOrange.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerBannerIndex = 7;
				bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);
			}
		});

		/*
		 * Panel with a horizontal layout to hold the Back and Continue buttons
		 */
		final UIPanel hpanel2 = new UIPanel();
		hpanel2.setLayout(new RowLayout(true));
		hpanel2.setBorder(SOLID_BORDER);
		vpanel.add(hpanel2);

		/*
		 * Back button
		 */
		final UIButton backButton = new UIButton("[b]Back[/b]");
		backButton.setStyledText(true);
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_CHOOSE_RACE_MENU;
			}
		});
		hpanel2.add(backButton);

		/*
		 * Continue button
		 */
		final UIButton continueButton = new UIButton("[b]Continue[/b]");
		continueButton.setStyledText(true);
		continueButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_CHOOSE_LEADER_NAME_MENU;
			}
		});
		hpanel2.add(continueButton);

		/*
		 * Finalization
		 */
		this.setName("Choose Banner");
		this.setContentPanel(vpanel);
		super.setUpFrame();
	}

	/**
	 * Update the contents of this frame before showing it.
	 */
	public void update() {
		UIPanel vpanel = this.getContentPanel();

		UILabel raceImageLabel = (UILabel) vpanel.getChild(0);
		raceImageLabel.setIcon(SUBTEX_RACE_HEADS[gameData.playerRaceIndex]);

		UIPanel hpanel = (UIPanel) vpanel.getChild(1);
		UILabel bannerImageLabel = (UILabel) hpanel.getChild(0);
		bannerImageLabel.setIcon(SUBTEX_BANNERS[gameData.playerBannerIndex]);

		UIPanel grid = (UIPanel) hpanel.getChild(1);
		UIRadioButton chooseBannerBlue = (UIRadioButton) grid.getChild(0);
		chooseBannerBlue.setSelected(gameData.playerBannerIndex == 0);
		UIRadioButton chooseBannerGreen = (UIRadioButton) grid.getChild(1);
		chooseBannerGreen.setSelected(gameData.playerBannerIndex == 1);
		UIRadioButton chooseBannerPurple = (UIRadioButton) grid.getChild(2);
		chooseBannerPurple.setSelected(gameData.playerBannerIndex == 2);
		UIRadioButton chooseBannerRed = (UIRadioButton) grid.getChild(3);
		chooseBannerRed.setSelected(gameData.playerBannerIndex == 3);
		UIRadioButton chooseBannerWhite = (UIRadioButton) grid.getChild(4);
		chooseBannerWhite.setSelected(gameData.playerBannerIndex == 4);
		UIRadioButton chooseBannerYellow = (UIRadioButton) grid.getChild(5);
		chooseBannerYellow.setSelected(gameData.playerBannerIndex == 5);
	}

}
